﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using IronBrokers.InformationBroker.ClientManagerService;
using System.Windows.Input;
using System.Windows;

namespace IronBrokers.InformationBroker.Model
{
    public class StockWindowViewModel : ViewModelBase
    {
        #region Public properties
        public ObservableCollection<TreeNode> Indexes
        {
            get
            {
                ObservableCollection<TreeNode> nodes = new ObservableCollection<TreeNode>();
                IEnumerable<String> indexesList = Values.Select(x => x.IndexName).Distinct();
                foreach (String indexName in indexesList)
                {
                    TreeNode indexNode = new TreeNode(indexName, TreeNodeType.Index);
                    indexNode.Children = new List<TreeNode>();
                    IEnumerable<String> goodsList = Values.Where(x => x.IndexName.Equals(indexName)).Select(x => x.GoodName).Distinct();
                    foreach (String goodsName in goodsList)
                    {
                        TreeNode goodsNode = new TreeNode(goodsName, TreeNodeType.Goods);
                        goodsNode.Parent = indexNode;
                        indexNode.Children.Add(goodsNode);
                    }
                    nodes.Add(indexNode);
                }
                return nodes;
            }
        }

        private TreeNode selectedNode = null;
        public TreeNode SelectedNode
        {
            get
            {
                return selectedNode;
            }
            set
            {
                selectedNode = value;
                if (value != null)
                {
                    if (value.NodeType == TreeNodeType.Goods)
                    {
                        IEnumerable<StockData> data = Values.Where(x => x.IndexName.Equals(selectedNode.Parent.Name) && x.GoodName.Equals(selectedNode.Name));
                        SelectedValues = new ObservableCollection<StockData>(data.OrderByDescending(x => x.Timestamp));
                        NewStockData.IndexName = value.Parent.Name;
                        NewStockData.GoodName = value.Name;
                    }
                    else if (value.NodeType == TreeNodeType.Index)
                    {
                        NewStockData.IndexName = value.Name;
                        NewStockData.GoodName = null;
                        SelectedValues = null;
                    }
                }
                else
                {
                    SelectedValues = null;
                }
                UpdateProperty("NewStockData");
                UpdateProperty("SelectedValues");
            }
        }

        public ObservableCollection<StockData> SelectedValues { get; private set; }

        public ObservableCollection<StockData> Values { get; private set; }
        public StockData NewStockData { get; set; } 
        #endregion

        #region Constructor
        public StockWindowViewModel()
        {
            Values = new ObservableCollection<StockData>();
            NewStockData = new StockData();
        } 
        #endregion

        #region Publish

        public ICommand PublishCommand
        {
            get
            {
                return new RelayCommand(param => Publish());
            }
        }

        public void Publish()
        {
            try
            {
                using (ClientManagerServiceClient svc = new ClientManagerServiceClient())
                {
                    Response response = svc.Publish(NewStockData);
                    if (response.Success)
                    {
                        NewStockData.Timestamp = DateTime.Now;
                        Values.Add(NewStockData);

                        NewStockData = new StockData();
                        UpdateProperty("NewStockData");
                        UpdateProperty("Indexes");
                        UpdateProperty("SelectedValues");
                    }
                    else
                    {
                        throw new Exception(response.ErrorMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred during publishing stock data.\n" + ex.Message);
            }
        } 

        #endregion
    }
}
